package com.kh.rationaldrugreview.mapper;

import com.kh.rationaldrugreview.entity.DrugManual;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Mapper for drug manual operations
 * 药品说明书Mapper接口
 */
@Mapper
public interface DrugManualMapper {
    
    /**
     * Insert a new drug manual record
     */
    int insert(DrugManual drugManual);
    
    /**
     * Update drug manual by id
     */
    int updateById(DrugManual drugManual);
    
    /**
     * Update status by id
     */
    int updateStatus(@Param("id") Long id, 
                     @Param("status") String status, 
                     @Param("errorMessage") String errorMessage);
    
    /**
     * Find by id
     */
    DrugManual findById(@Param("id") Long id);
    
    /**
     * Find by acceptance number
     */
    DrugManual findByAcceptanceNo(@Param("acceptanceNo") String acceptanceNo);
    
    /**
     * Check if manual exists by acceptance number
     */
    boolean existsByAcceptanceNo(@Param("acceptanceNo") String acceptanceNo);
    
    /**
     * Find all manuals
     */
    List<DrugManual> findAll();
    
    /**
     * Find manuals by status
     */
    List<DrugManual> findByStatus(@Param("status") String status);
    
    /**
     * Find manuals pending download
     */
    List<DrugManual> findPendingDownload(@Param("limit") int limit);
    
    /**
     * Find manuals pending parse
     */
    List<DrugManual> findPendingParse(@Param("limit") int limit);
    
    /**
     * Find manuals with errors for retry
     */
    List<DrugManual> findErrorsForRetry(@Param("maxRetryCount") int maxRetryCount, 
                                        @Param("limit") int limit);
    
    /**
     * Search manuals by criteria
     */
    List<DrugManual> search(@Param("drugName") String drugName,
                            @Param("companyName") String companyName,
                            @Param("drugType") String drugType,
                            @Param("status") String status,
                            @Param("startDate") LocalDate startDate,
                            @Param("endDate") LocalDate endDate,
                            @Param("offset") int offset,
                            @Param("limit") int limit);
    
    /**
     * Count manuals by status
     */
    int countByStatus(@Param("status") String status);
    
    /**
     * Update local path after download
     */
    int updateLocalPath(@Param("id") Long id, 
                       @Param("localPath") String localPath,
                       @Param("fileSize") Long fileSize);
    
    /**
     * Update parsed content
     */
    int updateParsedContent(@Param("id") Long id, 
                           @Param("parsedContent") String parsedContent,
                           @Param("structuredData") String structuredData,
                           @Param("pageCount") Integer pageCount);
    
    /**
     * Increment retry count
     */
    int incrementRetryCount(@Param("id") Long id);
    
    /**
     * Get statistics
     */
    DrugManualStatistics getStatistics();
    
    /**
     * Statistics DTO
     */
    class DrugManualStatistics {
        public int totalCount;
        public int pendingCount;
        public int downloadedCount;
        public int parsedCount;
        public int errorCount;
        public LocalDateTime lastSyncTime;
    }
    
    /**
     * Soft delete by id
     */
    int softDelete(@Param("id") Long id);
    
    /**
     * Find manuals updated after a specific time
     */
    List<DrugManual> findUpdatedAfter(@Param("timestamp") LocalDateTime timestamp);
}